<?php
 
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LeaveofficeController extends Controller
{

    //===================添加=================
    public function getAdd(){
    	//解析添加模板页面
    	return view('Personnel/Leaveoffice/add');
    }
    //执行添加操作
    public function postInsert(Request $request)
    {
        $name = session('username');
        $res = DB::table('Person')
        ->where('PersonName','=',$name)
        ->first();
    	 $data =$request ->all();
         $data['PersonID'] =$res['id'];
         //dd($data);
    	 //执行数据添加
    	 $res =DB::table('leaveoffice') ->insert($data);
    	 if($res){
    	 	//成功跳转列表页
    	 	return redirect('Personnel/Leaveoffice/index')->with('success','添加成功');
    	 }else{
    	 	return back() ->with('error','添加失败');
    	 }

    }
	//=========================显示=====================
    public function getIndex(Request $request){
    	//查询分页
	    	if($request ['name']){
	    		//搜索分页
	    		$res = DB::table('leaveoffice')
                ->join('person','leaveoffice.PersonID','=','person.id')
               
                ->where('person.PersonName','like','%'.$request['name'].'%')
                 ->select('Leaveoffice.*','PersonName')
                ->paginate(1);
	    	}else{
	    		//查询所有分页
	    		$res = DB::table('leaveoffice')
                ->join('person','leaveoffice.PersonID','=','person.id')
                ->select('Leaveoffice.*','PersonName')
                ->paginate(1);
	    	}
            //dd($res);
	    $info = $request -> all();
    	
    	//解析列表模板页面
	    return view('Personnel/Leaveoffice/index',['res'=>$res,'info'=>$info]);
    }

    //=================================编辑=====================
    public function getEdit($LeaveOfficeID){
       //dd($id);
        //根据id查询员工数据信息
        $info= DB::table('leaveoffice')
        ->join('person','person.id','=','leaveoffice.personID')
        ->where('LeaveOfficeID',$LeaveOfficeID)->get();
        //dd($info);
         $name = session('id');
               if($name!=$info[0]['id']){
                return back()->with('error','不是本人');
               }
    	//解析模板显示编辑的页面
        	return view('Personnel/Leaveoffice.edit',['info'=>$info,'LeaveOfficeID'=>$LeaveOfficeID]);
    }
    //================执行编辑
    public function postUpdate(Request $request){
        $info = $request ->all();
        //dd($info);
        $LeaveOfficeID = $info['LeaveOfficeID'];
        DB::table('leaveoffice')
        ->join('person','person.id','=','leaveoffice.personID')
        ->where('LeaveOfficeID',$LeaveOfficeID)->update($info);
        return redirect('Personnel/Leaveoffice/index')->with('修改成功');
    }   
   







}
?>